package com.lge.pitchingcoins.ui.clients;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lge.pitchingcoins.R;
import com.lge.pitchingcoins.ui.clients.playgame.PlayGameForClientsActivity;
import com.lge.pitchingcoins.util.PitchingCoinsLog;
import com.lge.pitchingcoins.util.sessions.ServiceHelper;

import java.util.ArrayList;

@Deprecated
public class GameConnectForClientsActivity extends Activity implements OnItemClickListener {
    public static int NOTIFY_DATA_SET_CHANGED = 1000;

    private ServiceHelper serviceHelper = null;
    private boolean isStartDiscover = false;
    
    private String clientNickname = null;

    private ListView serverListView = null;
    private ArrayList<NsdServiceInfo> serverList = null;
    private ServerNameListAdapter serverNameListAdapter = null;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            if (msg.what == NOTIFY_DATA_SET_CHANGED) {
                serverNameListAdapter.notifyDataSetChanged();
            }
        }
    };

    
    // Callback methods
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_game_connect_for_clients);

        initComponent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (serviceHelper != null) {
            if (!isStartDiscover) {
                isStartDiscover = true;
                serviceHelper.discoverServices();
            }
        }
    }

    @Override
    protected void onPause() {
        if (serviceHelper != null) {
            if (isStartDiscover) {
                stopDiscoverAndClearList();
            }
        }

        super.onPause();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        serviceHelper.reloveService(serverList.get(position));
    }

    public void onServiceFound(NsdServiceInfo serviceInfo) {
        for (NsdServiceInfo serviceInfoFromList : serverList) {
            if (serviceInfoFromList.equals(serviceInfo)) {
                return;
            }
        }

        if (serviceInfo != null) {
            PitchingCoinsLog.d(serviceInfo.getServiceName() + " founded");
            serverList.add(serviceInfo);
            handler.sendEmptyMessage(NOTIFY_DATA_SET_CHANGED);
        }
    }

    public void onServiceLost(NsdServiceInfo serviceInfo) {
        restartDiscoverService();
    }

    public void onServiceResolved(NsdServiceInfo serviceInfo) {
        if (serviceInfo != null) {
            startGameActivity(serviceInfo);
        }
    }


    // Member Methods
    public void initComponent() {
        serverList = new ArrayList<NsdServiceInfo>();

        serverListView = (ListView) findViewById(R.id.serverListView);
        serviceHelper = new ServiceHelper(this);

        serviceHelper.initializeNsd();

        serverNameListAdapter = new ServerNameListAdapter(this);
        serverListView.setAdapter(serverNameListAdapter);
        serverListView.setOnItemClickListener(this);
        
        clientNickname = getIntent().getExtras().getString("nickname");
    }

    public void stopDiscoverAndClearList() {
        isStartDiscover = false;
        serviceHelper.stopDiscovery();

        serverList.clear();
        handler.sendEmptyMessage(NOTIFY_DATA_SET_CHANGED);
    }

    public void restartDiscoverService() {
        if (serviceHelper != null) {
            if (isStartDiscover) {
                stopDiscoverAndClearList();
                serviceHelper.discoverServices();
                isStartDiscover = true;
            }
        }
    }

    public void startGameActivity(NsdServiceInfo serviceInfo) {
        Intent intent = new Intent(GameConnectForClientsActivity.this, PlayGameForClientsActivity.class);
        intent.putExtra("ipAddress", serviceInfo.getHost().getHostAddress());
        intent.putExtra("port", serviceInfo.getPort());
        intent.putExtra("nickname", clientNickname);
        startActivity(intent);
    }

    
    // Inner Classes
    class ServerNameListAdapter extends BaseAdapter {
        private Context mContext = null;
        private TextView serverNameTextView = null;

        public ServerNameListAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return serverList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            View row = view;

            if (row == null) {
                row = LayoutInflater.from(mContext).inflate(R.layout.adapter_server_name_list, null);
            }

            serverNameTextView = (TextView)row.findViewById(R.id.serverNameTextView);
            serverNameTextView.setText(serverList.get(position).getServiceName());

            return row;
        }
    }
}